package cmj.cloudmusic.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.musicmodule.Callback.Callback;
import com.example.musicmodule.entity.Music;
import com.example.musicmodule.util.MusicUrlUtil;
import com.example.musicmodule.util.RequestUtil;

import java.util.ArrayList;

import cmj.cloudmusic.R;
import cmj.cloudmusic.adapter.Music_ListView_Adapter;


/**
 * Created by 王虎 on 2016/12/15.
 */

public class TodayMusicFragment extends Fragment implements View.OnClickListener {

    String TAG = "cmj";


    Context context;
    View view;

    ImageButton back;

    ListView listView;
    ArrayList<Music> list;

    @Override
    public void onAttach(Context context) {
        this.context=context;
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.todaymusic,null);
        initView();
        initData();
        return view;
    }
    private void initView(){
        back= (ImageButton) view.findViewById(R.id.back);
        listView= (ListView) view.findViewById(R.id.listview);

        back.setOnClickListener(this);
    }
    private void initData(){
        list=new ArrayList<>();
        final RequestUtil requestUtil=new RequestUtil(context, new Callback() {
            @Override
            public void callback(String s) {
                JSONObject object= JSON.parseObject(s);
                JSONArray array=object.getJSONArray("song_list");
                list.addAll(JSON.parseArray(array.toJSONString(), Music.class));
                initListView();
            }
        });
        requestUtil.getData(MusicUrlUtil.getLoverMusic());

    }

    private void initListView(){
        RelativeLayout linearLayout= (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.todaymusic_head,null);
        listView.addHeaderView(linearLayout);
        Music_ListView_Adapter adapter=new Music_ListView_Adapter(context,list);
        listView.setAdapter(adapter);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:

                break;
        }
    }


}
